# 服务器专用启动脚本 - D:\Service\Lzfy_His_Service
param(
    [switch]$Console,
    [switch]$Service,
    [switch]$Stop,
    [switch]$Status,
    [int]$Port = 6731
)

$serverPath = "D:\Service\Lzfy_His_Service"
Set-Location $serverPath

Write-Host "=== 服务器HIS退费服务管理 ===" -ForegroundColor Green
Write-Host "服务路径: $serverPath" -ForegroundColor Cyan
Write-Host "当前时间: $(Get-Date)" -ForegroundColor Yellow

function Start-ConsoleMode {
    Write-Host "启动控制台模式..." -ForegroundColor Green
    
    # 检查服务是否已安装并运行
    $service = Get-Service -Name "LzfyHisService" -ErrorAction SilentlyContinue
    if ($service -and $service.Status -eq 'Running') {
        Write-Host "警告: Windows服务已在运行，建议先停止服务" -ForegroundColor Yellow
        Write-Host "运行: Stop-Service -Name LzfyHisService" -ForegroundColor Cyan
    }
    
    # 设置环境变量
    $env:ASPNETCORE_ENVIRONMENT = "Production"
    $env:ASPNETCORE_URLS = "http://0.0.0.0:$Port"
    
    Write-Host "启动参数: Port=$Port, Environment=Production" -ForegroundColor Cyan
    Write-Host "日志文件: $serverPath\logs\service-$(Get-Date -Format 'yyyyMMdd').log" -ForegroundColor Cyan
    Write-Host "按 Ctrl+C 停止服务..." -ForegroundColor Yellow
    Write-Host "================================" -ForegroundColor Green
    
    # 启动服务
    dotnet Lzfy_His_Service.dll
}

function Start-ServiceMode {
    Write-Host "启动Windows服务模式..." -ForegroundColor Green
    
    $service = Get-Service -Name "LzfyHisService" -ErrorAction SilentlyContinue
    if (-not $service) {
        Write-Host "服务未安装，请先安装服务" -ForegroundColor Red
        Write-Host "运行: .\install-service-server.ps1" -ForegroundColor Yellow
        return
    }
    
    if ($service.Status -eq 'Running') {
        Write-Host "服务已在运行中" -ForegroundColor Green
    } else {
        Start-Service -Name "LzfyHisService"
        Write-Host "服务启动成功" -ForegroundColor Green
    }
    
    # 显示服务状态
    Get-Service -Name "LzfyHisService" | Format-Table Name, Status, StartType
}

function Stop-ServiceMode {
    Write-Host "停止服务..." -ForegroundColor Yellow
    
    # 停止Windows服务
    $service = Get-Service -Name "LzfyHisService" -ErrorAction SilentlyContinue
    if ($service -and $service.Status -eq 'Running') {
        Stop-Service -Name "LzfyHisService" -Force
        Write-Host "Windows服务已停止" -ForegroundColor Green
    }
    
    # 停止控制台进程
    $processes = Get-Process -Name "dotnet" -ErrorAction SilentlyContinue
    foreach ($process in $processes) {
        $cmdLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$($process.Id)").CommandLine
        if ($cmdLine -like "*Lzfy_His_Service*") {
            Stop-Process -Id $process.Id -Force
            Write-Host "控制台进程已停止" -ForegroundColor Green
        }
    }
}

function Get-ServiceStatus {
    Write-Host "检查服务状态..." -ForegroundColor Cyan
    
    # 检查Windows服务
    $service = Get-Service -Name "LzfyHisService" -ErrorAction SilentlyContinue
    if ($service) {
        Write-Host "Windows服务:" -ForegroundColor Cyan
        $service | Format-Table Name, Status, StartType
    } else {
        Write-Host "Windows服务未安装" -ForegroundColor Yellow
    }
    
    # 检查控制台进程
    $processes = Get-Process -Name "dotnet" -ErrorAction SilentlyContinue
    $running = $false
    foreach ($process in $processes) {
        $cmdLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$($process.Id)").CommandLine
        if ($cmdLine -like "*Lzfy_His_Service*") {
            Write-Host "控制台进程: PID $($process.Id) 正在运行" -ForegroundColor Green
            $running = $true
        }
    }
    
    if (-not $running) {
        Write-Host "控制台进程未运行" -ForegroundColor Yellow
    }
    
    # 检查端口
    $portInUse = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
    if ($portInUse) {
        Write-Host "端口 $Port 正在使用" -ForegroundColor Green
    } else {
        Write-Host "端口 $Port 未使用" -ForegroundColor Yellow
    }
    
    # 显示日志文件信息
    $logPath = Join-Path $serverPath "logs\service-$(Get-Date -Format 'yyyyMMdd').log"
    if (Test-Path $logPath) {
        Write-Host "日志文件: $logPath" -ForegroundColor Cyan
        Write-Host "日志大小: $([math]::Round((Get-Item $logPath).Length / 1MB, 2)) MB" -ForegroundColor Cyan
    }
}

# 执行操作
switch ($true) {
    $Console { Start-ConsoleMode }
    $Service { Start-ServiceMode }
    $Stop { Stop-ServiceMode }
    $Status { Get-ServiceStatus }
    default {
        Write-Host "使用方法:" -ForegroundColor Cyan
        Write-Host "  .\server-start.ps1 -Console    # 控制台模式启动" -ForegroundColor Yellow
        Write-Host "  .\server-start.ps1 -Service     # Windows服务模式启动" -ForegroundColor Yellow
        Write-Host "  .\server-start.ps1 -Stop        # 停止所有服务" -ForegroundColor Yellow
        Write-Host "  .\server-start.ps1 -Status      # 查看服务状态" -ForegroundColor Yellow
        Write-Host "  .\server-start.ps1 -Console -Port 8080  # 指定端口" -ForegroundColor Yellow
    }
}